/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.Task;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author kylin
 */
public class TestJavaFXTask2 extends Application{
    
    MyCopyTask myTask;

    @Override
    public void start(Stage primaryStage) throws Exception {
        
        ProgressBar progressBarRunnable = new ProgressBar();
        progressBarRunnable.setProgress(0);
        CopyTaskRunnable myRunnable = new CopyTaskRunnable(progressBarRunnable);
        Thread myRunnableThread = new Thread(myRunnable);
        myRunnableThread.start();
        
        
        ProgressBar progressBarTimerTask = new ProgressBar();
        progressBarTimerTask.setProgress(0);
        myTask = new MyCopyTask(progressBarTimerTask);
        Timer myTimer = new Timer();
        myTimer.scheduleAtFixedRate(myTask, 0, 100);
                
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(new Label("Run in Thread(Task)"),
                new Label("Run in Thread(Runnable)"),
                progressBarRunnable,
                new Label("Run in Timer and TimerTask"),
                progressBarTimerTask
        );
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
 
        Scene scene = new Scene(root, 300, 250);
 
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        //copy();
        launch(args);
    }
    
    class CopyTaskRunnable implements Runnable{
        
        ProgressBar bar;
        long size =0;
        int length = 0;
        long cLength = 0;
        int MAX = 100;
        
        public CopyTaskRunnable(ProgressBar b){
            this.bar=b;
        }

        @Override
        public void run() {
            File file = new File("/home/kylin/Shared/www.zip");
            size = file.length();
            int bufferedsize=(int) (size / (Math.random() * 50 + MAX - 50));
            byte[] bts = new byte[bufferedsize];
            System.out.println("buffersize: "+bufferedsize);
            length = 0;
            cLength = 0;
            try{
		InputStream stream = new FileInputStream(file);
		OutputStream os = new FileOutputStream(new File("/home/kylin/Shared/8421.zip"));
		while((length = stream.read(bts)) > 0){
                    os.write(bts, 0, length);
                    cLength += length;
                    Platform.runLater(new Runnable(){
                        @Override
                        public void run() {
                            bar.setProgress((int) (cLength / size));
                        }
                    });
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(TestJavaFXTask.class.getName()).log(Level.SEVERE, null, ex);
                    }  
		}
                os.flush();
		os.close();
		stream.close();
                System.out.println("finished!");
            }
            catch(Exception e){
                e.printStackTrace();
            }   
        }
    }
    class MyCopyTask extends TimerTask{
        int MAX=100;
        ProgressBar bar;
        double count;
        long size =0;
        int length = 0;
        long cLength = 0;
        

        public MyCopyTask(ProgressBar b) {
            bar = b;
            count = 0;
        }

        @Override
        public void run() {

//            bar.setProgress(count++/MAX);
//            
//            if(count >= MAX){
//                myTask.cancel();
//            }
            File file = new File("/home/kylin/Shared/www.zip");
            size = file.length();
            int bufferedsize=(int) (size / (Math.random() * 50 + MAX - 50));
            byte[] bts = new byte[bufferedsize];
            System.out.println("buffersize: "+bufferedsize);
            length = 0;
            cLength = 0;
            try{
		InputStream stream = new FileInputStream(file);
		OutputStream os = new FileOutputStream(new File("/home/kylin/Shared/8422.zip"));
		while((length = stream.read(bts)) > 0){
                    os.write(bts, 0, length);
                    cLength += length;
                    
                    bar.setProgress((int) (cLength * MAX / size));
                    
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(TestJavaFXTask.class.getName()).log(Level.SEVERE, null, ex);
                    }  
		}
                os.flush();
		os.close();
		stream.close();
                System.out.println("finished!");
                myTask.cancel();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            
        }
        
    }
}
